package com.ycsj.goldmedalnewconcept.activity;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.Classinfo;
import com.ycsj.goldmedalnewconcept.bean.ClassinfoResponse;
import com.ycsj.goldmedalnewconcept.bean.TeacherInfo;
import com.ycsj.goldmedalnewconcept.bean.TeacherListResponse;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//老师列表
public class TeacherListActivity extends Activity implements OnClickListener {
	
	private GridView gv_filemanage;
	private FileManageAdapter mAdapter;
	private List<TeacherInfo> list;
	private String account;
	private String role;
	private String teachermobile;
	private Intent intent;
	private String view_type;
	private String class_id;
	private String teachername;
	private TextView tv_title;
	private ImageView iv_back;
    
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_teacherlist);
		account = SPUtil.getString(this, "account", "");
		role = SPUtil.getString(this, "role", "");
		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		gv_filemanage = (GridView) findViewById(R.id.gv_filemanage);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		
		
	}

	private void initData() {
		OkHttpClient mOkHttpClient = new OkHttpClient();
//		mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "15");
		builder.add("c", account);
		builder.add("role", role);
//		http://192.168.0.164:8080/ycsj_count/MessageCenter?type=15&c=18672626702&role=3
		Request request = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter)
				.post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {
				
			@Override		
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
					}
				});
			}
 
			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();
				TeacherListResponse fromJson = gson.fromJson(
						arg0.body().string(), TeacherListResponse.class);
				list = fromJson.list;
				
				
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						if(list!=null){
							mAdapter = new FileManageAdapter(list,getBaseContext());
							gv_filemanage.setAdapter(mAdapter);
							gv_filemanage.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {
									if(list!=null){
										teachermobile = list.get(position).userid;
										teachername = list.get(position).name;
										SPUtil.putString(getApplicationContext(), "teachermobile", teachermobile);
										SPUtil.putString(getApplicationContext(), "teachername", teachername);
										finish();
									}
								}
							});
						}
					}
				});
			}
		});
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
			}
	}
	
	class FileManageAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private List<TeacherInfo> list;

		public FileManageAdapter(List<TeacherInfo> list,Context context) {
			super();
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			if (null != list) {
				return list.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.gv_filemanage_item, null);
				viewHolder = new ViewHolder();
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				viewHolder.icon = (ImageView) convertView
						.findViewById(R.id.image);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.name.setText(list.get(position).name);
//			viewHolder.icon.setImageResource(list.get(position)
//					.getIcon());
			Picasso.with(getBaseContext())
			.load(list.get(position).icon).error(R.drawable.header)
			.into(viewHolder.icon);
			return convertView;
		}
	}

	class ViewHolder {
		public TextView name;
		public ImageView icon;
	}
}
